<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - code analysis - /disk1/DNNAcc/gemmlowp/profiling/instrumentation.h</title>
  <link rel="stylesheet" type="text/css" href="../../../../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../../../../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../../../../index.html">top level</a> - <a href="index.html">disk1/DNNAcc/gemmlowp/profiling</a> - instrumentation.h<span style="font-size: 80%;"> (source / <a href="instrumentation.h.func-sort-c.html">functions</a>)</span></td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">code analysis</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">3</td>
            <td class="headerCovTableEntry">5</td>
            <td class="headerCovTableEntryLo">60.0 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2020-09-17 13:31:02</td>
            <td></td>
            <td class="headerItem">Functions:</td>
            <td class="headerCovTableEntry">1</td>
            <td class="headerCovTableEntry">1</td>
            <td class="headerCovTableEntryHi">100.0 %</td>
          </tr>
          <tr>
            <td class="headerItem">Legend:</td>
            <td class="headerValueLeg">            Lines:
            <span class="coverLegendCov">hit</span>
            <span class="coverLegendNoCov">not hit</span>
</td>
            <td></td>
          </tr>
          <tr><td><img src="../../../../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../../../../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<a name="1"><span class="lineNum">       1 </span>            : // Copyright 2015 The Gemmlowp Authors. All Rights Reserved.</a>
<span class="lineNum">       2 </span>            : //
<span class="lineNum">       3 </span>            : // Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<span class="lineNum">       4 </span>            : // you may not use this file except in compliance with the License.
<span class="lineNum">       5 </span>            : // You may obtain a copy of the License at
<span class="lineNum">       6 </span>            : //
<span class="lineNum">       7 </span>            : //     http://www.apache.org/licenses/LICENSE-2.0
<span class="lineNum">       8 </span>            : //
<span class="lineNum">       9 </span>            : // Unless required by applicable law or agreed to in writing, software
<span class="lineNum">      10 </span>            : // distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<span class="lineNum">      11 </span>            : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<span class="lineNum">      12 </span>            : // See the License for the specific language governing permissions and
<span class="lineNum">      13 </span>            : // limitations under the License.
<span class="lineNum">      14 </span>            : 
<span class="lineNum">      15 </span>            : // instrumentation.h: contains the definitions needed to
<span class="lineNum">      16 </span>            : // instrument code for profiling:
<span class="lineNum">      17 </span>            : //   ScopedProfilingLabel, RegisterCurrentThreadForProfiling.
<span class="lineNum">      18 </span>            : //
<span class="lineNum">      19 </span>            : // profiler.h is only needed to drive the profiler:
<span class="lineNum">      20 </span>            : //   StartProfiling, FinishProfiling.
<span class="lineNum">      21 </span>            : //
<span class="lineNum">      22 </span>            : // See the usage example in profiler.h.
<span class="lineNum">      23 </span>            : 
<span class="lineNum">      24 </span>            : #ifndef GEMMLOWP_PROFILING_INSTRUMENTATION_H_
<span class="lineNum">      25 </span>            : #define GEMMLOWP_PROFILING_INSTRUMENTATION_H_
<span class="lineNum">      26 </span>            : 
<span class="lineNum">      27 </span>            : #include &lt;cstdio&gt;
<span class="lineNum">      28 </span>            : 
<span class="lineNum">      29 </span>            : #ifndef GEMMLOWP_USE_STLPORT
<span class="lineNum">      30 </span>            : #include &lt;cstdint&gt;
<span class="lineNum">      31 </span>            : #else
<span class="lineNum">      32 </span>            : #include &lt;stdint.h&gt;
<span class="lineNum">      33 </span>            : namespace std {
<span class="lineNum">      34 </span>            : using ::int16_t;
<span class="lineNum">      35 </span>            : using ::int32_t;
<span class="lineNum">      36 </span>            : using ::int8_t;
<span class="lineNum">      37 </span>            : using ::size_t;
<span class="lineNum">      38 </span>            : using ::uint16_t;
<span class="lineNum">      39 </span>            : using ::uint32_t;
<span class="lineNum">      40 </span>            : using ::uint8_t;
<span class="lineNum">      41 </span>            : using ::uintptr_t;
<span class="lineNum">      42 </span>            : }  // namespace std
<span class="lineNum">      43 </span>            : #endif
<span class="lineNum">      44 </span>            : 
<span class="lineNum">      45 </span>            : #include &lt;algorithm&gt;
<span class="lineNum">      46 </span>            : #include &lt;cassert&gt;
<span class="lineNum">      47 </span>            : #include &lt;cstdlib&gt;
<span class="lineNum">      48 </span>            : 
<span class="lineNum">      49 </span>            : #ifdef GEMMLOWP_PROFILING
<span class="lineNum">      50 </span>            : #include &lt;cstring&gt;
<span class="lineNum">      51 </span>            : #include &lt;set&gt;
<span class="lineNum">      52 </span>            : #endif
<span class="lineNum">      53 </span>            : 
<span class="lineNum">      54 </span>            : #include &quot;./pthread_everywhere.h&quot;
<span class="lineNum">      55 </span>            : 
<a name="56"><span class="lineNum">      56 </span>            : namespace gemmlowp {</a>
<span class="lineNum">      57 </span>            : 
<span class="lineNum">      58 </span><span class="lineCov">   24923978 : inline void ReleaseBuildAssertion(bool condition, const char* msg) {</span>
<span class="lineNum">      59 </span><span class="lineCov">   24923978 :   if (!condition) {</span>
<span class="lineNum">      60 </span><span class="lineNoCov">          0 :     fprintf(stderr, &quot;gemmlowp error: %s\n&quot;, msg);</span>
<span class="lineNum">      61 </span><span class="lineNoCov">          0 :     abort();</span>
<span class="lineNum">      62 </span>            :   }
<span class="lineNum">      63 </span><span class="lineCov">   24923978 : }</span>
<span class="lineNum">      64 </span>            : 
<span class="lineNum">      65 </span>            : class Mutex {
<span class="lineNum">      66 </span>            :  public:
<span class="lineNum">      67 </span>            :   Mutex(const Mutex&amp;) = delete;
<span class="lineNum">      68 </span>            :   Mutex&amp; operator=(const Mutex&amp;) = delete;
<span class="lineNum">      69 </span>            : 
<span class="lineNum">      70 </span>            :   Mutex() { pthread_mutex_init(&amp;m, NULL); }
<span class="lineNum">      71 </span>            :   ~Mutex() { pthread_mutex_destroy(&amp;m); }
<span class="lineNum">      72 </span>            : 
<span class="lineNum">      73 </span>            :   void Lock() { pthread_mutex_lock(&amp;m); }
<span class="lineNum">      74 </span>            :   void Unlock() { pthread_mutex_unlock(&amp;m); }
<span class="lineNum">      75 </span>            : 
<span class="lineNum">      76 </span>            :  private:
<span class="lineNum">      77 </span>            :   pthread_mutex_t m;
<span class="lineNum">      78 </span>            : };
<span class="lineNum">      79 </span>            : 
<span class="lineNum">      80 </span>            : class GlobalMutexes {
<span class="lineNum">      81 </span>            :  public:
<span class="lineNum">      82 </span>            :   static Mutex* Profiler() {
<span class="lineNum">      83 </span>            :     static Mutex m;
<span class="lineNum">      84 </span>            :     return &amp;m;
<span class="lineNum">      85 </span>            :   }
<span class="lineNum">      86 </span>            : 
<span class="lineNum">      87 </span>            :   static Mutex* EightBitIntGemm() {
<span class="lineNum">      88 </span>            :     static Mutex m;
<span class="lineNum">      89 </span>            :     return &amp;m;
<span class="lineNum">      90 </span>            :   }
<span class="lineNum">      91 </span>            : };
<span class="lineNum">      92 </span>            : 
<span class="lineNum">      93 </span>            : // A very simple RAII helper to lock and unlock a Mutex
<span class="lineNum">      94 </span>            : struct ScopedLock {
<span class="lineNum">      95 </span>            :   ScopedLock(Mutex* m) : _m(m) { _m-&gt;Lock(); }
<span class="lineNum">      96 </span>            :   ~ScopedLock() { _m-&gt;Unlock(); }
<span class="lineNum">      97 </span>            : 
<span class="lineNum">      98 </span>            :  private:
<span class="lineNum">      99 </span>            :   Mutex* _m;
<span class="lineNum">     100 </span>            : };
<span class="lineNum">     101 </span>            : 
<span class="lineNum">     102 </span>            : // Profiling definitions. Two paths: when profiling is enabled,
<span class="lineNum">     103 </span>            : // and when profiling is disabled.
<span class="lineNum">     104 </span>            : #ifdef GEMMLOWP_PROFILING
<span class="lineNum">     105 </span>            : // This code path is when profiling is enabled.
<span class="lineNum">     106 </span>            : 
<span class="lineNum">     107 </span>            : // A pseudo-call-stack. Contrary to a real call-stack, this only
<span class="lineNum">     108 </span>            : // contains pointers to literal strings that were manually entered
<span class="lineNum">     109 </span>            : // in the instrumented code (see ScopedProfilingLabel).
<span class="lineNum">     110 </span>            : struct ProfilingStack {
<span class="lineNum">     111 </span>            :   static const std::size_t kMaxSize = 30;
<span class="lineNum">     112 </span>            :   typedef const char* LabelsArrayType[kMaxSize];
<span class="lineNum">     113 </span>            :   LabelsArrayType labels;
<span class="lineNum">     114 </span>            :   std::size_t size;
<span class="lineNum">     115 </span>            :   Mutex* lock;
<span class="lineNum">     116 </span>            : 
<span class="lineNum">     117 </span>            :   ProfilingStack() { memset(this, 0, sizeof(ProfilingStack)); }
<span class="lineNum">     118 </span>            :   ~ProfilingStack() { delete lock; }
<span class="lineNum">     119 </span>            : 
<span class="lineNum">     120 </span>            :   void Push(const char* label) {
<span class="lineNum">     121 </span>            :     ScopedLock sl(lock);
<span class="lineNum">     122 </span>            :     ReleaseBuildAssertion(size &lt; kMaxSize, &quot;ProfilingStack overflow&quot;);
<span class="lineNum">     123 </span>            :     labels[size] = label;
<span class="lineNum">     124 </span>            :     size++;
<span class="lineNum">     125 </span>            :   }
<span class="lineNum">     126 </span>            : 
<span class="lineNum">     127 </span>            :   void Pop() {
<span class="lineNum">     128 </span>            :     ScopedLock sl(lock);
<span class="lineNum">     129 </span>            :     ReleaseBuildAssertion(size &gt; 0, &quot;ProfilingStack underflow&quot;);
<span class="lineNum">     130 </span>            :     size--;
<span class="lineNum">     131 </span>            :   }
<span class="lineNum">     132 </span>            : 
<span class="lineNum">     133 </span>            :   void UpdateTop(const char* new_label) {
<span class="lineNum">     134 </span>            :     ScopedLock sl(lock);
<span class="lineNum">     135 </span>            :     assert(size);
<span class="lineNum">     136 </span>            :     labels[size - 1] = new_label;
<span class="lineNum">     137 </span>            :   }
<span class="lineNum">     138 </span>            : 
<span class="lineNum">     139 </span>            :   ProfilingStack&amp; operator=(const ProfilingStack&amp; other) {
<span class="lineNum">     140 </span>            :     memcpy(this, &amp;other, sizeof(ProfilingStack));
<span class="lineNum">     141 </span>            :     return *this;
<span class="lineNum">     142 </span>            :   }
<span class="lineNum">     143 </span>            : 
<span class="lineNum">     144 </span>            :   bool operator==(const ProfilingStack&amp; other) const {
<span class="lineNum">     145 </span>            :     return !memcmp(this, &amp;other, sizeof(ProfilingStack));
<span class="lineNum">     146 </span>            :   }
<span class="lineNum">     147 </span>            : };
<span class="lineNum">     148 </span>            : 
<span class="lineNum">     149 </span>            : static_assert(
<span class="lineNum">     150 </span>            :     !(sizeof(ProfilingStack) &amp; (sizeof(ProfilingStack) - 1)),
<span class="lineNum">     151 </span>            :     &quot;ProfilingStack should have power-of-two size to fit in cache lines&quot;);
<span class="lineNum">     152 </span>            : 
<span class="lineNum">     153 </span>            : struct ThreadInfo;
<span class="lineNum">     154 </span>            : 
<span class="lineNum">     155 </span>            : // The global set of threads being profiled.
<span class="lineNum">     156 </span>            : inline std::set&lt;ThreadInfo*&gt;&amp; ThreadsUnderProfiling() {
<span class="lineNum">     157 </span>            :   static std::set&lt;ThreadInfo*&gt; v;
<span class="lineNum">     158 </span>            :   return v;
<span class="lineNum">     159 </span>            : }
<span class="lineNum">     160 </span>            : 
<span class="lineNum">     161 </span>            : struct ThreadInfo {
<span class="lineNum">     162 </span>            :   pthread_key_t key;  // used only to get a callback at thread exit.
<span class="lineNum">     163 </span>            :   ProfilingStack stack;
<span class="lineNum">     164 </span>            : 
<span class="lineNum">     165 </span>            :   ThreadInfo() {
<span class="lineNum">     166 </span>            :     pthread_key_create(&amp;key, ThreadExitCallback);
<span class="lineNum">     167 </span>            :     pthread_setspecific(key, this);
<span class="lineNum">     168 </span>            :     stack.lock = new Mutex();
<span class="lineNum">     169 </span>            :   }
<span class="lineNum">     170 </span>            : 
<span class="lineNum">     171 </span>            :   static void ThreadExitCallback(void* ptr) {
<span class="lineNum">     172 </span>            :     ScopedLock sl(GlobalMutexes::Profiler());
<span class="lineNum">     173 </span>            :     ThreadInfo* self = static_cast&lt;ThreadInfo*&gt;(ptr);
<span class="lineNum">     174 </span>            :     ThreadsUnderProfiling().erase(self);
<span class="lineNum">     175 </span>            :   }
<span class="lineNum">     176 </span>            : };
<span class="lineNum">     177 </span>            : 
<span class="lineNum">     178 </span>            : inline ThreadInfo&amp; ThreadLocalThreadInfo() {
<span class="lineNum">     179 </span>            :   static pthread_key_t key;
<span class="lineNum">     180 </span>            :   static auto DeleteThreadInfo = [](void* threadInfoPtr) {
<span class="lineNum">     181 </span>            :     ThreadInfo* threadInfo = static_cast&lt;ThreadInfo*&gt;(threadInfoPtr);
<span class="lineNum">     182 </span>            :     if (threadInfo) {
<span class="lineNum">     183 </span>            :       delete threadInfo;
<span class="lineNum">     184 </span>            :     }
<span class="lineNum">     185 </span>            :   };
<span class="lineNum">     186 </span>            : 
<span class="lineNum">     187 </span>            :   // key_result is unused. The purpose of this 'static' local object is
<span class="lineNum">     188 </span>            :   // to have its initializer (the pthread_key_create call) performed exactly
<span class="lineNum">     189 </span>            :   // once, in a way that is guaranteed (since C++11) to be reentrant.
<span class="lineNum">     190 </span>            :   static const int key_result = pthread_key_create(&amp;key, DeleteThreadInfo);
<span class="lineNum">     191 </span>            :   (void)key_result;
<span class="lineNum">     192 </span>            : 
<span class="lineNum">     193 </span>            :   ThreadInfo* threadInfo = static_cast&lt;ThreadInfo*&gt;(pthread_getspecific(key));
<span class="lineNum">     194 </span>            :   if (!threadInfo) {
<span class="lineNum">     195 </span>            :     threadInfo = new ThreadInfo();
<span class="lineNum">     196 </span>            :     pthread_setspecific(key, threadInfo);
<span class="lineNum">     197 </span>            :   }
<span class="lineNum">     198 </span>            :   return *threadInfo;
<span class="lineNum">     199 </span>            : }
<span class="lineNum">     200 </span>            : 
<span class="lineNum">     201 </span>            : // ScopedProfilingLabel is how one instruments code for profiling
<span class="lineNum">     202 </span>            : // with this profiler. Construct local ScopedProfilingLabel variables,
<span class="lineNum">     203 </span>            : // passing a literal string describing the local code. Profile
<span class="lineNum">     204 </span>            : // samples will then be annotated with this label, while it is in scope
<span class="lineNum">     205 </span>            : // (whence the name --- also known as RAII).
<span class="lineNum">     206 </span>            : // See the example in profiler.h.
<span class="lineNum">     207 </span>            : class ScopedProfilingLabel {
<span class="lineNum">     208 </span>            :   ProfilingStack* profiling_stack_;
<span class="lineNum">     209 </span>            : 
<span class="lineNum">     210 </span>            :  public:
<span class="lineNum">     211 </span>            :   explicit ScopedProfilingLabel(const char* label)
<span class="lineNum">     212 </span>            :       : profiling_stack_(&amp;ThreadLocalThreadInfo().stack) {
<span class="lineNum">     213 </span>            :     profiling_stack_-&gt;Push(label);
<span class="lineNum">     214 </span>            :   }
<span class="lineNum">     215 </span>            : 
<span class="lineNum">     216 </span>            :   ~ScopedProfilingLabel() { profiling_stack_-&gt;Pop(); }
<span class="lineNum">     217 </span>            : 
<span class="lineNum">     218 </span>            :   void Update(const char* new_label) { profiling_stack_-&gt;UpdateTop(new_label); }
<span class="lineNum">     219 </span>            : };
<span class="lineNum">     220 </span>            : 
<span class="lineNum">     221 </span>            : // To be called once on each thread to be profiled.
<span class="lineNum">     222 </span>            : inline void RegisterCurrentThreadForProfiling() {
<span class="lineNum">     223 </span>            :   ScopedLock sl(GlobalMutexes::Profiler());
<span class="lineNum">     224 </span>            :   ThreadsUnderProfiling().insert(&amp;ThreadLocalThreadInfo());
<span class="lineNum">     225 </span>            : }
<span class="lineNum">     226 </span>            : 
<span class="lineNum">     227 </span>            : #else  // not GEMMLOWP_PROFILING
<span class="lineNum">     228 </span>            : // This code path is when profiling is disabled.
<span class="lineNum">     229 </span>            : 
<span class="lineNum">     230 </span>            : // This empty definition of ScopedProfilingLabel ensures that
<span class="lineNum">     231 </span>            : // it has zero runtime overhead when profiling is disabled.
<span class="lineNum">     232 </span>            : struct ScopedProfilingLabel {
<span class="lineNum">     233 </span>            :   explicit ScopedProfilingLabel(const char*) {}
<span class="lineNum">     234 </span>            :   void Update(const char*) {}
<span class="lineNum">     235 </span>            : };
<span class="lineNum">     236 </span>            : 
<span class="lineNum">     237 </span>            : inline void RegisterCurrentThreadForProfiling() {}
<span class="lineNum">     238 </span>            : 
<span class="lineNum">     239 </span>            : #endif
<span class="lineNum">     240 </span>            : 
<span class="lineNum">     241 </span>            : }  // end namespace gemmlowp
<span class="lineNum">     242 </span>            : 
<span class="lineNum">     243 </span>            : #endif  // GEMMLOWP_PROFILING_INSTRUMENTATION_H_
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../../../../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.12</a></td></tr>
  </table>
  <br>

</body>
</html>
